package com.ywd.platform.netty.jt080.util;

import com.ywd.platform.vo.PointVo;
import com.ywd.platform.vo.VehicleTrackRecordOutVo;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 *
 */
public class CoordinateConverter {
    // 圆周率
    private static final double PI = 3.1415926535897932384626;
    // 长半轴（WGS-84椭球体）
    private static final double A = 6378245.0;
    // 偏心率平方
    private static final double EE = 0.00669342162296594323;

    /**
     * WGS-84 转 GCJ-02
     * @param wgsLng WGS-84经度
     * @param wgsLat WGS-84纬度
     * @return GCJ-02坐标数组 [经度, 纬度]
     */
    public static double[] wgs84ToGcj02(double wgsLng, double wgsLat) {
        // 检查是否在中国境外
        if (outOfChina(wgsLng, wgsLat)) {
            return new double[]{wgsLng, wgsLat};
        }

        double dLat = transformLat(wgsLng - 105.0, wgsLat - 35.0);
        double dLng = transformLng(wgsLng - 105.0, wgsLat - 35.0);

        double radLat = wgsLat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - EE * magic * magic;

        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / (A * (1 - EE) / (magic * sqrtMagic) * PI);
        dLng = (dLng * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);

        return new double[]{wgsLng + dLng, wgsLat + dLat};
    }

    /**
     *
     * WGS-84 转 GCJ-02 （WGS-84加密而）
     * @param wgsLng WGS-84经度
     * @param wgsLat WGS-84纬度
     * @return GCJ-02坐标数组 [经度, 纬度]
     */
    public static VehicleTrackRecordOutVo wgs84ToGcj022(double wgsLng, double wgsLat) {
        double dLat = transformLat(wgsLng - 105.0, wgsLat - 35.0);
        double dLng = transformLng(wgsLng - 105.0, wgsLat - 35.0);

        double radLat = wgsLat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - EE * magic * magic;

        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / (A * (1 - EE) / (magic * sqrtMagic) * PI);
        dLng = (dLng * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);


        Double longitude = wgsLng + dLng;
        Double latitude = wgsLat + dLat;

        return new VehicleTrackRecordOutVo(null,new BigDecimal(longitude).setScale(14, RoundingMode.HALF_UP),
                new BigDecimal(latitude).setScale(14, RoundingMode.HALF_UP));
    }

    /**
     * 判断坐标是否在中国境外
     */
    private static boolean outOfChina(double lng, double lat) {
        return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271;
    }

    /**
     * 纬度转换计算
     */
    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 经度转换计算
     */
    private static double transformLng(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }


    public static void main(String[] args) {
        double[] doubles = wgs84ToGcj02(104.094407, 30.553883);
        System.out.println(doubles[0]);
        System.out.println(doubles[1]);
    }

}
